import os from 'os'


const dealTime = (seconds) => {
    seconds = seconds | 0;
    let day = (seconds / (3600 * 24)) | 0;
    let hours = ((seconds - day * 3600) / 3600) | 0;
    let minutes = ((seconds - day * 3600 * 24 - hours * 3600) / 60) | 0;
    let second = seconds % 60;
    (day < 10) && (day = '0' + day);
    (hours < 10) && (hours = '0' + hours);
    (minutes < 10) && (minutes = '0' + minutes);
    (second < 10) && (second = '0' + second);
    return [day, hours, minutes, second].join(':');
};

const dealMem = (mem) => {
    let G = 0,
        M = 0,
        KB = 0;
    (mem > (1 << 30)) && (G = (mem / (1 << 30)).toFixed(2));
    (mem > (1 << 20)) && (mem < (1 << 30)) && (M = (mem / (1 << 20)).toFixed(2));
    (mem > (1 << 10)) && (mem > (1 << 20)) && (KB = (mem / (1 << 10)).toFixed(2));
    return G > 0 ? G + 'G' : M > 0 ? M + 'M' : KB > 0 ? KB + 'KB' : mem + 'B';
};

//cpu架构
const arch = os.arch();
//操作系统内核
const kernel = os.type();
//操作系统平台
const pf = os.platform();
//系统开机时间
const uptime = os.uptime();
//主机名
const hn = os.hostname();
//主目录
const hdir = os.homedir();
//内存
const totalMem = os.totalmem();
const freeMem = os.freemem();
// console.log("内存大小：" + dealMem(totalMem) + ' 空闲内存：' + dealMem(freeMem));

//cpu信息
const cpus = os.cpus();
// console.log('*****cpu信息*******');
let cpuMes = cpus.reduce((m, cpu, idx) => {
    let times = cpu.times;
    let cpu_single = {
        cpucore: {
            nums: idx + 1,
            note: 'cpu核心'
        },
        cpumodel: {
            type: cpu.model,
            note: 'cpu型号'
        },
        speed: {
            speed: `${cpu.speed}MHz`,
            note: '频率'
        },
        rota: {
            rota: `${((1 - times.idle / (times.idle + times.user + times.nice + times.sys + times.irq)) * 100).toFixed(2)}%`,
            note: '使用率'
        }
    }
    m.push(cpu_single)
    return m
}, [])


//网卡
// console.log('*****网卡信息*******');
const networksObj = os.networkInterfaces();

export default {
    cpuFramework: {
        arch,
        note: "cpu架构"
    },
    operatingSystemKernel: {
        kernel,
        note: "操作系统内核"
    },
    platform: {
        pf,
        note: "平台"
    },
    bootUpTime: {
        time: dealTime(uptime),
        note: "开机时间"
    },
    hostName: {
        name: hn,
        note: "主机名"
    },
    homeDir: {
        dirname: hdir,
        note: "主目录"
    },
    memMes: {
        totalMem: {
            totalMem: dealMem(totalMem),
            note: "内存大小"
        },
        freeMem: {
            freeMem: dealMem(freeMem),
            note: "空闲内存"
        }
    },
    cpuMes: cpuMes,
    networksMes: networksObj
}